﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;

namespace StripeOne.Core.Ftp
{
    internal class StripeOneFtpClientStream : Stream
    {
        #region Attributes
        
        private FtpWebRequest _req;
		private FtpWebResponse _resp;
		private Stream _stream;
		private long _length;
		private long _position;

        #endregion

        #region Public Methods

        internal StripeOneFtpClientStream(FtpWebResponse resp, long length)
        {
            this._req = null;
            this._resp = resp;
            this._position = 0L;
            this._length = length;
            this._stream = resp.GetResponseStream();
        }

        internal StripeOneFtpClientStream(FtpWebRequest req, long length)
        {
            this._req = req;
            this._resp = null;
            this._position = 0L;
            this._length = length;
            this._stream = req.GetRequestStream();
        }

        public override bool CanRead
		{
			get
			{
				return this._stream.CanRead;
			}
		}
		
        public override bool CanSeek
		{
			get
			{
				return this._stream.CanSeek;
			}
		}
		
        public override bool CanWrite
		{
			get
			{
				return this._stream.CanWrite;
			}
		}
		
        public override long Length
		{
			get
			{
				return this._length;
			}
		}
		
        public override long Position
		{
			get
			{
				return this._position;
			}
			set
			{
				if (this._position != value)
				{
					throw new NotSupportedException();
				}
			}
		}
		
        public override void Flush()
		{
			this._stream.Flush();
		}
		
        public override void SetLength(long value)
		{
			this._length = this.Length;
			this._stream.SetLength(value);
		}
		
        public override int Read(byte[] buffer, int offset, int count)
		{
			int num = this._stream.Read(buffer, offset, count);
			this._position += (long)num;
			return num;
		}
		
        public override long Seek(long offset, SeekOrigin origin)
		{
			return this._stream.Seek(offset, origin);
		}
		
        public override void Write(byte[] buffer, int offset, int count)
		{
			this._stream.Write(buffer, offset, count);
		}
		
        public override void Close()
		{
			base.Close();
			this._stream.Close();
			if (this._req != null)
			{
				this._req.GetResponse();
			}
        }

        #endregion
    }
}
